﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;

/// <summary>
/// ImgCompression 的摘要说明
/// </summary>
public class ImgCompression
{
    public Image ResourceImage;
    public string ErrorMessage;
    public ImgCompression(string ImageFileName)
    {
        ResourceImage = Image.FromFile(ImageFileName);
        ErrorMessage = string.Empty;
    }
    public bool ThumbnailCallback()
    {
        return false;
    }


    /// <summary>
    /// 方法一：可以自定义图片的高度和宽度
    /// </summary>
    /// <param name="Width">图片宽度</param>
    /// <param name="Height">图片高度</param>
    /// <param name="targetFilePath">图片路径</param>
    /// <returns></returns>        
    public bool ReducedImage(int Width, int Height, string targetFilePath)
    {
        try
        {
            Image ReducedImage;
            Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);
            ReducedImage = ResourceImage.GetThumbnailImage(Width, Height, callb, IntPtr.Zero);
            ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);
            ReducedImage.Dispose();
            ResourceImage.Dispose();
            return true;
        }
        catch (Exception e)
        {
            ErrorMessage = e.Message;
            return false;
        }
    
    }
}